package com.zhongwang.business.module.baseCostManage.controller;

import com.zhongwang.business.base.BaseController;
import com.zhongwang.business.common.JsonResult;
import com.zhongwang.business.gen.entity.User;
import com.zhongwang.business.module.baseCostManage.model.WeightHoursModel;
import com.zhongwang.business.module.baseCostManage.service.WeightHoursService;
import com.zhongwang.business.util.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *  控制层
 * @author China.wzy
 */
@Slf4j
@Controller
@RequestMapping("/dataEntry/designTime")
public class WeightHoursController extends BaseController {
    @Autowired
    private WeightHoursService weighthoursService;

    //路径
//    private String prefix = "/dataEntry";

//    @GetMapping("/designTime")
//    public  String designTime(Model model){
//        // 获取登入用户实体
//        User user = ShiroUtils.getUser();
//        model.addAttribute("userName", user.getName());
//        model.addAttribute("odrDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
//        return prefix + "/designTime";
//    }


    /**
     * 加载人工工时
     */
    @ResponseBody
    @PostMapping("/design")
    public JsonResult design(WeightHoursModel weighthoursmodel){
        List<WeightHoursModel> list = weighthoursService.designHours(weighthoursmodel);
        return jsonResult(list);
    }

    /**
     * 加载设备重量
     */
    @ResponseBody
    @PostMapping("/weight")
    public JsonResult weight(WeightHoursModel weighthoursmodel){
        List<WeightHoursModel>list=weighthoursService.designWeight(weighthoursmodel);
        return jsonResult(list);
    }
    /**
     * 保存工时、重量数据
     * @param design
     */
    @ResponseBody
    @PostMapping("/hoursWeightSave")
    public  JsonResult saveData(WeightHoursModel design){
        JsonResult jsonResult = new JsonResult();
        int flg =weighthoursService.saveHoursWeight(design);
        if (flg>0){
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setData(flg);
        }else {
            jsonResult.setType(JsonResult.ResultType.ERROR);
        }
        return jsonResult;
    }
    /**
     * 工时模态框显示
     */
    @ResponseBody
    @PostMapping("/showHours")
    public WeightHoursModel selectHoursOne(Integer desId){
        return weighthoursService.selectHoursOne(desId);
    }

    /**
     * 重量模态框显示
     */
    @ResponseBody
    @PostMapping("/showWeight")
    public WeightHoursModel selectWeightOne(Integer weiId){
        return weighthoursService.selectWeightOne(weiId);
    }

    /**
     * 删除工时数据
     */
    @ResponseBody
    @PostMapping("/delHours")
    public JsonResult delHours(Integer desId){
        return jsonResult(weighthoursService.delHouseOne(desId));
    }

    /**
     *删除重量数据
     */
    @ResponseBody
    @PostMapping("/delWeight")
    public JsonResult delWeight(Integer weiId){
        return jsonResult(weighthoursService.delWeightOne(weiId));
    }

    /**工时数据清空*/
    @ResponseBody
    @PostMapping("/DesHoursDel")
    public JsonResult selectDesHoursDel(WeightHoursModel weighthoursmodel){
        int row = weighthoursService.selDesHoursDel(weighthoursmodel);
        if (row == -1){
            return error("数据已被接收，无法进行删除！！需联系商务科");
        }else if (row == 0){
            return error("无数据，操作失败");
        }else if (row == 1){
            return  jsonResult(weighthoursService.deleteDesHoursAll(weighthoursmodel));
        }else {
                return JsonResult.fail();
        }
    }

    /**重量数据清空*/
    @ResponseBody
    @PostMapping("/DesWeightDel")
    public JsonResult selectDesWeightDel(WeightHoursModel weighthoursmodel){
        int row = weighthoursService.selDesWeightDel(weighthoursmodel);
        if (row == -1){
            return error("数据已被接收，无法进行删除！！需联系商务科");
        }else if (row == 0){
            return error("无数据，操作失败");
        }else if (row == 1){
            return jsonResult(weighthoursService.deleteDesWeightAll(weighthoursmodel));
        }else {
            return JsonResult.fail();
        }
    }
}